<template>
	<div style="background-color: #F2F2F2;">
		<div class="examination">
			<div class="choose" :class="type == 0 ? 'chooseColor' : ''" @click="tabs(0)">
				<div>详情</div>
				<div :class="type == 0 ? 'chooseActive' : 'chooseActive2'"></div>
			</div>
			<div class="choose" :class="type == 1 ? 'chooseColor' : ''" @click="tabs(1)">
				<div>目录</div>
				<div :class="type == 1 ? 'chooseActive' : 'chooseActive2'"></div>
			</div>
		</div>

		<div v-if="type == 0">
			<div class="navs">
				<div>
					<div>{{ courseName }}</div>
					<div class="title2" style="text-align: right;">{{ createTime }}</div>
				</div>
			</div>
			<div class="navs">
				<div class="title">课程介绍</div>
				<div v-html="imageText"></div>
			</div>
		</div>

		<div v-if="type == 1 && list[0].chapterId">
			<div class="navs" v-for="(item, index) in list" :key="index" @click="detail(item.chapterId,index)">
				<div class="title">{{ item.chapterName }}</div>
				<div class="vue-btn title2">
					<div>{{ item.chapterTime }}</div>
					<div>{{ item.learnNum }}人已学习</div>
				</div>
			</div>
		</div>

		<!-- 紧急事件弹窗 -->
		<div v-if="popup">
			<div @click="hidden" class="modal-mask"></div>
			<div class="navs2">
				<div class="title">章节详情:</div>
				<div class="html-img" v-html="msgHtml"></div>
			</div>
		</div>
	</div>
</template>

<script>
import { courseInfo, finishChapter } from '../../../../network/api';
export default {
	name: 'studyDetail',
	data() {
		return {
			id: '', //课程id
			type: 0,
			courseName: '', //课程标题
			imageText: '', //课程富文本简介
			createTime: '', //课程创建时间

			msgHtml:'',//小节里的内容
			list: [],
			popup: false
		};
	},
	components: {},
	created() {},
	mounted() {
		this.id = this.$route.query.id;
		this.getMsg();
	},
	methods: {
		// 课程信息
		getMsg() {
			let param = {
				courseId: this.$route.query.id
			};
			courseInfo(param).then(res => {
				// console.log(res);
				// 宽度100%，高度自适应
				// res.retData.chapterList.forEach(item => {
				// 	item.chapterImageText = item.chapterImageText.replace(/\<img/gi, '<img style="max-width:100%;height:auto;" ');
				// });
				// res.retData.imageText = res.retData.imageText.replace(/\<img/gi, '<img style="max-width:100%;height:auto;" ');

				this.courseName = res.retData.courseName;
				this.createTime = res.retData.createTime;
				this.imageText = res.retData.imageText;
				this.list = res.retData.chapterList;
				// if(this.list[0].chapterId){
				// 	this.chapterImageText = this.list[0].chapterImageText
				// }
			});
		},
		// 切换分类
		tabs(e) {
			this.type = e;
		},
		// 章节切换
		detail(id, index) {
			// var that = this;
			this.msgHtml = this.list[index].chapterImageText
			let param = {
				courseId: this.id,
				chapterId: id,
				userId: localStorage.getItem('userid')
			};
			finishChapter(param).then(res => {
				// 请求成功,重置
				this.popup = true;
			});
		},
		// 隐藏
		hidden() {
			this.popup = false;
			this.getMsg();
		}
	}
};
</script>

<style lang="scss" scoped>
.examination {
	display: flex;
	justify-content: space-around;
	align-items: center;
	background-color: #ffffff;
}

.choose {
	text-align: center;
	padding-top: 12px;
}
.chooseColor {
	color: #cb1c1d;
}
.chooseActive {
	color: #cb1c1d;
	background-color: #cb1c1d;
	width: 36px;
	height: 4px;
	border-radius: 10px;
	margin-top: 8px;
}
.chooseActive2 {
	color: #000000;
	background-color: #ffffff;
	width: 36px;
	height: 4px;
	border-radius: 10px;
	margin-top: 8px;
}

/* type=0 */
.navs {
	background-color: #ffffff;
	margin-top: 10px;
	padding: 10px 15px;
	line-height: 160%;
	text-align: left;
}
.title {
	font-size: 16px;
	margin-bottom: 7px;
}
.time {
	margin-bottom: 5px;
}
.title2 {
	color: #999999;
}
.icon {
	width: 16px;
	height: 16px;
	margin-right: 5px;
}
.title3 {
	color: #cc1f20;
}
.white {
	white-space: nowrap;
}
/* 弹窗 */
.modal-mask {
	width: 100%;
	height: 100%;
	position: fixed;
	top: 0%;
	left: 0%;
	right: 0%;
	bottom: 0%;
	background: #000;
	opacity: 0.7;
	overflow: hidden;
	color: #fff;
	z-index: 3;
}

.navs2 {
	width: 60%;
	height: 600px;
	background-color: #fff;
	border-radius: 20rpx 20rpx 0 0;
	position: fixed;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	z-index: 4;
	padding: 20rpx 30rpx;
	box-sizing: border-box;
	color: #330033;
	padding: 30px;
	text-align: left;
	overflow-y: scroll;
	border-radius: 3px;
}

.html-img /deep/ img,p,span {
  width: 100%;
}
</style>
